import Koa = require('koa');
import path = require('path');
import koastatic=require('koa-static');
import Router = require('koa-router');
const app = new Koa();

//文件上传
const koaBody = require('koa-body');
app.use(koaBody({
    multipart: true,
    formidable: {
        maxFileSize: 200*1024*1024    // 设置上传文件大小最大限制，默认2M
    }
}));

const bodyParser = require('koa-bodyparser')
app.use(bodyParser());
const router = new Router();

//路由跳转前先进行token验证
const check = require('./config/tokenCheck')
router.use(check)

//---路由---
//登录
var loginRouter=require(__dirname+'/router/login/loginRouter');
//系统管理
var systemRouter=require(__dirname+'/router/system/systemRouter');

router.use('/login', loginRouter.routes());
router.use('/system', systemRouter.routes());


app.use(router.routes());

//文件上传
const multer=require('koa-multer')
//配置
var storage = multer.diskStorage({
    //文件保存路径
    destination: function (req, file, cb) {
        cb(null, '../uploads/')
    },
    //修改文件名称
    filename: function (req, file, cb) {
        var fileFormat = (file.originalname).split(".");  //以点分割成数组，数组的最后一项就是后缀名
        cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})
// //加载配置
// var upload = multer({ storage: storage });
// router.post('/upload',upload.single('file'),async(ctx,next)=>{
//     // console.log( ctx.req['file']);
//     ctx.body = {
//         // filename: 'http://localhost:83/' + ctx.req['file'].filename//返回文件名
//         filename: 'http://t.esbus.cn/' + ctx.req['file'].filename//返回文件名
//     }
// })


//解决vue页面刷新 Not Found
import history from "koa2-connect-history-api-fallback"
//这句代码需要在koa-static上面
app.use(history());
app.use(koastatic(path.join(__dirname, './public')));


//配置静态目录
app.use(koastatic(path.join(__dirname).replace("dist","public")));
// app.use(koastatic(path.join(__dirname).replace("dist","uploads")));
app.use(koastatic(path.join(process.cwd(),'uploads')));
app.listen(8022);